﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;


namespace weiqijiance.数据库操作
{
    public class dbbass
    {
        /// <summary>
        /// 数据库配置
        public string sDataBaseIp;
        public string sDataBaseName;
        public string sDataBaseUserName;
        public string sDataBasePwd;
        public dbbass()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\数据库设置.ini";
            string path1 = string.Format(@"{0}", path);
            iniclass objini = new iniclass();
            objini.Ini(path1);
            sDataBaseIp = objini.ReadValue("设置", "数据库IP");
            sDataBaseName = objini.ReadValue("设置", "数据库名称");
            sDataBaseUserName = objini.ReadValue("设置", "用户");
            sDataBasePwd = objini.ReadValue("设置", "密码");
        }

        //public virtual void getSQLSZ()
        //{


        //}

        ////数据库操作部分
        public virtual bool InsertDB(string sql)
        {
            if (sDataBaseIp.Length < 1)
                return false;
            string connectstr = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};", sDataBaseIp, sDataBaseName, sDataBaseUserName, sDataBasePwd);
            SqlConnection sqlcnn = new SqlConnection(connectstr);
            try
            {
                //打开数据库
                sqlcnn.Open();
                SqlCommand cmd = new SqlCommand(sql, sqlcnn);
                //执行插入操作，并返回受影响的行数
                int i = cmd.ExecuteNonQuery();
                //如果受影响的行数小于一则说明操作失败了
                if (i < 1)
                    return false;
                //关闭数据库
                sqlcnn.Close();
            }
            catch (Exception er)
            {
                sqlcnn.Close();
                string temp = er.Message.ToString();
               // MessageBox.Show(temp);
                return false;
            }
            return true;
        }

        ////数据库操作部分
        public virtual bool InsertDBquxian(string sql, byte[] bt)
        {
            if (sDataBaseIp.Length < 1)
                return false;
            string connectstr = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};", sDataBaseIp, sDataBaseName, sDataBaseUserName, sDataBasePwd);
            SqlConnection sqlcnn = new SqlConnection(connectstr);
            //try
            {
                //打开数据库
                sqlcnn.Open();
                SqlCommand cmd = new SqlCommand(sql, sqlcnn);
                cmd.Parameters.AddWithValue("@vaxbrimage", bt);
                //执行插入操作，并返回受影响的行数
                int i = cmd.ExecuteNonQuery();
                //如果受影响的行数小于一则说明操作失败了
                if (i < 1)
                    return false;
                //关闭数据库
                sqlcnn.Close();
            }
            //catch (Exception er)
            //{
            //    string temp = er.Message.ToString();
            //    MessageBox.Show(temp);
            //    return false;
            //}
            return true;
        }
        public virtual int selectDBcount(string sql)//查询该SQL语句的返回结果
        {
            if (sDataBaseIp.Length < 1)
                return 0;
            int icount = 0;

            try
            {
                string connectionstring = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};", sDataBaseIp, sDataBaseName, sDataBaseUserName, sDataBasePwd);
                SqlConnection cnn = new SqlConnection(connectionstring);
                cnn.Open();
                SqlCommand cmd = new SqlCommand(sql, cnn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                DataTable dt = ds.Tables[0];//结果在这个table里，把它作为数据源就好了
                cnn.Close();
                icount = ds.Tables[0].Rows.Count;
                return icount;
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.ToString());
                return 0;
            }

        }
        public virtual string GetDBString(string sql, string DBname)
        {
            if (sDataBaseIp.Length < 1)
                return "";
            string m_stest;
            m_stest = "";
            try
            {
                string connectionstring = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};", sDataBaseIp, sDataBaseName, sDataBaseUserName, sDataBasePwd);
                SqlConnection cnn = new SqlConnection(connectionstring);
                cnn.Open();
                SqlCommand cmd = new SqlCommand(sql, cnn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                System.Data.DataTable dt = ds.Tables[0];//结果在这个table里，把它作为数据源就好了
                foreach (DataRow myDataRow in dt.Rows)
                {

                    m_stest = myDataRow[DBname].ToString().Trim();
                }
                cnn.Close();

            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.ToString());
                return m_stest;

            }
            return m_stest;
        }
        public virtual int GetDBInt(string sql, string DBname)
        {
            if (sDataBaseIp.Length < 1)
                return 0;
            try
            {
                string connectionstring = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};", sDataBaseIp, sDataBaseName, sDataBaseUserName, sDataBasePwd);
                SqlConnection cnn = new SqlConnection(connectionstring);
                cnn.Open();
                SqlCommand cmd = new SqlCommand(sql, cnn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                DataTable dt = ds.Tables[0];//结果在这个table里，把它作为数据源就好了
                foreach (DataRow myDataRow in dt.Rows)
                {

                    if (myDataRow[DBname].ToString().Trim() != "")
                    {
                        return int.Parse(myDataRow[DBname].ToString().Trim());
                    }
                    else
                    {
                        return 0;
                    }
                }
                cnn.Close();

            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.ToString());
                return 0;

            }
            return 0;
        }

        public string getsqlmessage(string stable, string smessage)//获取SQL数据中对应信息
        {
            string ssqlex = "";
            ssqlex = string.Format(@"select * from {0} where table_2='{1}'", stable, smessage);
            DataTable dt = GetTable(ssqlex);
            if (dt.Rows.Count > 0)
            {

                return dt.Rows[0]["table_1"].ToString().Trim();
            }
            else
            {
                return "";
            }

        }
        public virtual DataTable GetTable(string sSQL)
        {
            DataTable dttt = new DataTable();
            if (sDataBaseIp.Length < 1)
                return dttt;
            try
            {
                string connectionstring = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};", sDataBaseIp, sDataBaseName, sDataBaseUserName, sDataBasePwd);
                SqlConnection sqlconn = new SqlConnection(connectionstring);
                sqlconn.Open();
                SqlCommand cmd = new SqlCommand(sSQL, sqlconn);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                sqlconn.Close();
                return ds.Tables[0];
            }
            catch (Exception)
            {
                //MessageBox.Show(ex.Message);
                return dttt;
            }
        }
        ///
    }
}
